// 刷新页面
import Router from '@/router'
let Base64 = require('js-base64').Base64
export const $routerRedirect = () => {
  let {
    path,
    query
  } = Router.history.current
  Router.replace('/redirect?path=' + path + `&query=${JSON.stringify(query)}`)
}

// kkFile地址
export const $kkFileFn = (url) => {
  // 以前的
  // let origin = process.env.NODE_ENV === 'development' ? 'http://192.168.1.205' : window.location.origin
  // let fileUrl = origin + '/file/' + url
  // let previewUrl = origin + '/preview/onlinePreview?url=' + fileUrl
  // window.open(previewUrl)


  // // 线下
  // let origin = process.env.NODE_ENV === 'development' ? 'http://192.168.1.205' : window.location.origin
  // let fileUrl = origin + '/file/' + url
  // let previewUrl = origin + '/preview/onlinePreview?url=' + fileUrl

  // 线上
  let o = encodeURIComponent(Base64.encode('http://118.122.86.3:8888/group1/' + url)) //线上
  let previewUrl = 'http://118.122.86.3:8012/onlinePreview?url=' + o
  window.open(previewUrl)
}

// 如果是下载基设局的资源文件,则需要调用此函数
export const $download = function (url, name) {
  // let origin = process.env.NODE_ENV === 'development' ? 'http://192.168.1.205' : window.location.origin
  // let downloadUrl = origin + '/download/' + url + '?attname=' + name
  // $iframeDownload(downloadUrl)
  url = url.slice(4)
  let origin = process.env.NODE_ENV === 'development' ? 'http://118.122.86.3:8200' : 'http://118.122.86.3:8200'
  let downloadUrl = origin + '/data/' + url + '?attname=' + name
  download(downloadUrl)
}

// 默认下载
export function $iframeDownload(url) {
  const iframe = document.createElement('iframe')
  iframe.style.display = 'none'

  function iframeLoad() {
    console.log('iframe onload')
    const win = iframe.contentWindow
    const doc = win.document
    if (win.location.href === url) {
      if (doc.body.childNodes.length > 0) {
        // response is error
      }
      iframe.parentNode.removeChild(iframe)
    }
  }
  if ('onload' in iframe) {
    iframe.onload = iframeLoad
  } else if (iframe.attachEvent) {
    iframe.attachEvent('onload', iframeLoad)
  } else {
    iframe.onreadystatechange = function onreadystatechange() {
      if (iframe.readyState === 'complete') {
        iframeLoad()
      }
    }
  }
  iframe.src = ''
  document.body.appendChild(iframe)

  setTimeout(function loadUrl() {
    iframe.contentWindow.location.href = url
  }, 50)
}

// 将1,234,567.00转换为1234567.00\

export const $moneyToNumValue = val => {
  var num = val == '0' ? val : val.toString().trim()
  var ss = num.toString()
  if (ss.length == 0) {
    return '0'
  }
  return ss.replace(/,/g, '')
}

// 调用：fmoney("12345.675910", 3)，返回12,345.676
export const $formattMoney = (s, n = 2) => {
  s = !s ? 0 : s
  n = n > 0 && n <= 20 ? n : 2
  s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + ''
  var l = s.split('.')[0].split('').reverse()
  var r = s.split('.')[1]
  let t = ''
  for (let i = 0; i < l.length; i++) {
    t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? ',' : '')
  }
  let arr = t.split('')
  if (arr.includes('-')) {
    arr[arr.length - 2] = ''
  }
  return arr.reverse().join('') + '.' + r
}
